package com.dongfg.zcmserver.aspect;

import com.dongfg.zcmserver.config.Config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Check Api Version.
 * Created by 13071472 on 2015/11/9.
 */
@Aspect
@Component
public class ApiVersionCheckAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiVersionCheckAspect.class);

    @Pointcut("execution(* com.dongfg.zcmserver.controller.ApiController.*(..))")
    public void pointCut() {

    }

    @Around("pointCut()")
    public Object invoke(ProceedingJoinPoint invocation) throws Throwable {
        Method method = ((MethodSignature) invocation.getSignature()).getMethod();

        Object[] args = invocation.getArgs();
        if (!Config.API_VERSION_V1.equals(args[0])) {
            return new ResponseEntity<>("API版本错误", HttpStatus.BAD_REQUEST);
        }

        LOGGER.info("Invoke Method {},API Version {}.", method.getName(), args[0]);
        return invocation.proceed();
    }
}
